/**  
* @Title: BannerServiceImpl.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年8月9日  
*/
package com.fortune.rainshinesystem.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.system.BannerModel;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.util.StringUtil;
import com.fortune.rainshinesystem.mapper.BannerMapper;
import com.fortune.rainshinesystem.service.BannerService;

/**  
* @ClassName: BannerServiceImpl  
* @Description: TODO(这里用一句话描述这个类的作用)  
* @author 张鑫
* @phone 17561783855
* @date 2019年8月9日  
*    
*/
@Service
public class BannerServiceImpl implements BannerService {
    private static final Logger logger = LoggerFactory.getLogger(BannerServiceImpl.class);
    @Autowired
    private BannerMapper mapper;
    
	@Override
	public MsgModel add(BannerModel model) {
		try {
            int res = mapper.insert(model);
            if (res > 0) {
                return new MsgModel("banner图上传成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("banner图上传失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("add ====> {}", e);
            return new MsgModel("banner图上传失败！", ResultCode.FAILURE);
        }
	}

	@Override
	public MsgModel list(ParmsModel parm) {
		try {
            Wrapper<BannerModel> wrapper = new EntityWrapper<>();
            if (StringUtil.isNotBlank(parm.getSearchKey())) {
                wrapper.like(parm.getSearchKey(), parm.getSearchValue());
            }
            Page<BannerModel> page = new Page<>(parm.getPage(), parm.getLimit());
            List<BannerModel> list = mapper.selectPage(page, wrapper.orderBy("create_time", true));
            return new MsgModel(page.getTotal(), list);
        } catch (Exception e) {
            logger.error("cardList ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
	}

	@Override
	public MsgModel update(BannerModel model) {
		try {
            if(model.getCreateId() != null){
                model.setUpdateId(model.getCreateId());
            }
            int res = mapper.updateAllColumnById(model);
            if (res > 0) {
                return new MsgModel("更新Banner图成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("更新Banner图失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("updateCard ====> {}", e);
            return new MsgModel("更新Banner图失败！", ResultCode.FAILURE);
        }
	}
	
    @Override
    public MsgModel delete(BannerModel model) {
        try {
            boolean rs = mapper.deleteById(model.getId()) > 0;
            if (rs) {
                return new MsgModel("删除Banner图成功！", ResultCode.SUCCESS);
            } else {
                return new MsgModel("删除Banner图失败！", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error("delCard ====> {}", e);
            return new MsgModel("删除Banner图失败！", ResultCode.FAILURE);
        }
    }

}
